package logic

import (
	"context"
	"zg_four_week3/esclient/internal/svc"
	"zg_four_week3/esclient/internal/types"
	"zg_four_week3/esserver/esserverclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type EsAppendLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEsAppendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EsAppendLogic {
	return &EsAppendLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *EsAppendLogic) EsAppend(req *types.EsAppendRequest) (resp *types.EsAppendResponse, err error) {
	// todo: add your logic here and delete this line
	index := req.Index
	title := req.Title
	data := req.Data
	num := req.Num
	var r types.EsAppendResponse
	r.Code = -1
	_, err = l.svcCtx.EsServer.EsAppend(l.ctx, &esserverclient.EsAppendRequest{
		Index: index,
		Data:  data,
		Num:   int64(num),
		Title: title,
	})
	if err != nil {
		return &r, nil
	}
	r.Code = 200
	r.Msg = "success"
	return &r, nil
}
